package top.ffhome.concurrent.beautiful.ch01;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

public class WaitNotifyIAllTest {
    private static final Log log = LogFactory.get();

    private static volatile Object obj = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(() -> {
            synchronized (obj) {
                log.info("threadA get obj lock");
                try {
                    log.info("threadA begin wait");
                    obj.wait();
                    log.info("threadA end wait");
                } catch (InterruptedException e) {
                    log.error(e);
                }
            }
        });

        Thread threadB = new Thread(() -> {
            synchronized (obj) {
                log.info("threadB get obj lock");
                try {
                    log.info("threadB begin wait");
                    obj.wait();
                    log.info("threadB end wait");
                } catch (InterruptedException e) {
                    log.error(e);
                }
            }
        });

        Thread threadC = new Thread(() -> {
            synchronized (obj) {
                log.info("threadC begin notify");
                obj.notifyAll();
            }
        });

        threadA.start();
        threadB.start();
        Thread.sleep(1000);
        threadC.start();

        threadA.join();
        threadB.join();
        threadC.join();
        log.info("main thread end");
    }
}
